package cn.lili.modules.promotion.serviceimpl;

import cn.lili.modules.promotion.entity.dos.GreenConfig;
import cn.lili.modules.promotion.entity.dto.GreenConfigValueDTO;
import cn.lili.modules.promotion.mapper.GreenConfigMapper;
import cn.lili.modules.promotion.service.GreenConfigService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.annotations.ApiModelProperty;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;

/**
 * @author xiangqian
 * @version 1.0
 * @data 2023/12/18 14:17
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class GreenConfigServiceImpl extends ServiceImpl<GreenConfigMapper, GreenConfig> implements GreenConfigService {

    /**
     * 查询最新功能配置
     */
    @Override
    public GreenConfig getNewConfig() {
        LambdaQueryWrapper<GreenConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(GreenConfig::getCreateTime);
        queryWrapper.last("limit 1");
        return this.getOne(queryWrapper);
    }

    @Override
    public GreenConfigValueDTO getNewGreenConfigValueDTO() {
        GreenConfig greenConfig = getNewConfig();
        if(greenConfig == null || StringUtils.isEmpty(greenConfig.getConfigValue())){
            return null;
        }
        log.info("greenConfig---{}", JSON.toJSONString(greenConfig));
        GreenConfigValueDTO greenConfigValueDTO = JSONObject.parseObject(greenConfig.getConfigValue(), GreenConfigValueDTO.class);
        log.info("GreenConfigValueDTO---{}", JSON.toJSONString(greenConfigValueDTO));
        return greenConfigValueDTO;
    }

    /**
     * 查询分红时间
     */
    @Override
    public String getConfigTime() {
        GreenConfig greenConfig = getNewConfig();
        if(greenConfig == null || StringUtils.isEmpty(greenConfig.getConfigValue())){
            return null;
        }
        GreenConfigValueDTO greenConfigValueDTO = JSONObject.parseObject(greenConfig.getConfigValue(), GreenConfigValueDTO.class);
        return greenConfigValueDTO.getConfig();
    }

    /**
     * 查询兑换值领取奖金手续费
     * */
    @Override
    public BigDecimal getExchangevalue() {
        GreenConfig greenConfig = getNewConfig();
        if(greenConfig == null || StringUtils.isEmpty(greenConfig.getConfigValue())){
            return null;
        }
        GreenConfigValueDTO greenConfigValueDTO = JSONObject.parseObject(greenConfig.getConfigValue(), GreenConfigValueDTO.class);
        return greenConfigValueDTO.getHandlingcharge().getExchangevalue();
    }

    /**
     * 查询分红手续费
     * */
    @Override
    public BigDecimal getDividendvalue() {
        GreenConfig greenConfig = getNewConfig();
        if(greenConfig == null || StringUtils.isEmpty(greenConfig.getConfigValue())){
            return null;
        }
        GreenConfigValueDTO greenConfigValueDTO = JSONObject.parseObject(greenConfig.getConfigValue(), GreenConfigValueDTO.class);
        return greenConfigValueDTO.getHandlingcharge().getDividendvalue();
    }
}
